//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Resource Application Block
//===============================================================================

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Configuration.Design;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using EntLibContrib.Resource.Providers;

namespace EntLibContrib.Resource.Configuration.Design
{
	class ResourceManagerSettingsBuilder
	{
		#region Fields
		private ResourceManagerSettingsNode resourceSettingsNode;
		private IConfigurationUIHierarchy hierarchy;
		private ResourceManagerSettings resourceConfiguration;
		#endregion

		#region Construction
		/// <summary>
		/// Initializes a new instance of the <see cref="ResourceManagerSettingsBuilder"/> class.
		/// </summary>
		/// <param name="serviceProvider">The service provider.</param>
		/// <param name="resourceSettingsNode">The resource settings node.</param>
		public ResourceManagerSettingsBuilder (IServiceProvider serviceProvider, ResourceManagerSettingsNode resourceSettingsNode)
		{
			this.resourceSettingsNode = resourceSettingsNode;
			hierarchy = ServiceHelper.GetCurrentHierarchy(serviceProvider);
			resourceConfiguration = new ResourceManagerSettings();
		}
		#endregion

		#region Public Methods
		/// <summary>
		/// Builds the configuration data for the resource manager settings.
		/// </summary>
		/// <returns></returns>
		public ResourceManagerSettings Build ()
		{
			resourceConfiguration.DefaultResourceManager = resourceSettingsNode.DefaultResourceManager.Name;

			BuildResourceProviders();

			BuildResourceManagers();

			return resourceConfiguration;
		}
		#endregion

		#region Private Methods
		private void BuildResourceManagers ()
		{
			foreach ( ResourceManagerNode managerNode in hierarchy.FindNodesByType(resourceSettingsNode, typeof(ResourceManagerNode)) )
			{
				ResourceManagerData resourceManagerData = managerNode.ResourceManagerData;
				ResourceProviderNode providerNodeForManager = (ResourceProviderNode)hierarchy.FindNodeByType(managerNode, typeof(ResourceProviderNode));
				resourceManagerData.ResourceProvider = (providerNodeForManager == null) ? InternalResources.AssemblyResourceProviderName : providerNodeForManager.Name;
				string resourceBaseName = (providerNodeForManager == null) ? String.Empty : providerNodeForManager.ResourceProviderData.ResourceBaseName;

				if (resourceManagerData.ResourceProvider == InternalResources.AssemblyResourceProviderName && !resourceConfiguration.ResourceProviders.Contains(InternalResources.AssemblyResourceProviderName))
				{
					resourceConfiguration.ResourceProviders.Add(new ResourceProviderData(InternalResources.AssemblyResourceProviderName, resourceBaseName, typeof(AssemblyResourceProvider)));
				}

				resourceConfiguration.ResourceManagers.Add(resourceManagerData);
			}
		}

		private void BuildResourceProviders ()
		{
			foreach ( ResourceProviderNode resourceProviderNode in hierarchy.FindNodesByType(resourceSettingsNode, typeof(ResourceProviderNode)) )
			{
				ResourceProviderData resourceProviderData = resourceProviderNode.ResourceProviderData;

				resourceConfiguration.ResourceProviders.Add(resourceProviderData);
			}
		}
		#endregion
	}
}